﻿using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace GlobalizationTools.UI
{
    public class DataGridView : DataGrid
    {
        static DataGridView()
        {
            ColumnsContextProperty = DependencyProperty.Register("ColumnsContext", typeof(ObservableCollection<DataGridColumn>), typeof(DataGridView), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridView.OnColumnsContextChanged)));
        }

        public DataGridView()
        {
        }

        private static void OnColumnsContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((DataGridView)d).OnColumnsContextChanged((ObservableCollection<DataGridColumn>)e.OldValue, (ObservableCollection<DataGridColumn>)e.NewValue);
        }

        NotifyCollectionChangedEventHandler newColumns_CollectionChangedEventHandler;
        int _startingColumnsContext = -1;

        private void OnColumnsContextChanged(ObservableCollection<DataGridColumn> oldColumns, ObservableCollection<DataGridColumn> newColumns)
        {
            if (_startingColumnsContext == -1)
                _startingColumnsContext = Columns.Count;
            if (oldColumns != null)
            {
                if (newColumns_CollectionChangedEventHandler != null)
                {
                    oldColumns.CollectionChanged -= newColumns_CollectionChangedEventHandler;
                }
                foreach (DataGridColumn column in oldColumns)
                {
                    if (Columns.Contains(column))
                    {
                        Columns.Remove(column);
                    }
                }
            }
            if (newColumns != null)
            {
                newColumns_CollectionChangedEventHandler = new NotifyCollectionChangedEventHandler(newColumns_CollectionChanged);
                newColumns.CollectionChanged += newColumns_CollectionChangedEventHandler;
                foreach (DataGridColumn column in newColumns)
                {
                    Columns.Add(column);
                }
            }
        }

        void newColumns_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (_startingColumnsContext == -1)
                _startingColumnsContext = Columns.Count;
            if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                while (Columns.Count > _startingColumnsContext)
                {
                    Columns.RemoveAt(_startingColumnsContext);
                }
            }
            if (e.OldItems != null)
            {
                foreach (DataGridColumn column in e.OldItems)
                {
                    if (Columns.Contains(column))
                    {
                        Columns.Remove(column);
                    }
                }
            }
            if (e.NewItems != null)
            {
                foreach (DataGridColumn column in e.NewItems)
                {
                    Columns.Add(column);
                }
            }
        }

        public ObservableCollection<DataGridColumn> ColumnsContext
        {
            get
            {
                return (ObservableCollection<DataGridColumn>)base.GetValue(ColumnsContextProperty);
            }
            set
            {
                base.SetValue(ColumnsContextProperty, value);
            }
        }

        public static readonly DependencyProperty ColumnsContextProperty;

        
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Down:
                case Key.Up:
                    base.OnKeyDown(e);
                    break;
                default:
                    base.OnPreviewKeyDown(e);
                    break;
            }
        }



    }
}
